<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./lib/bootstrap3.3.7.css">
    <script src="./lib/moment.2.24.0.js"></script>
    <script src="./lib/vue2.4.4.js"></script>
    <!-- `vue-resource` 依赖于 `vue` -->
    <script src="./lib/vue-resource1.3.4.js"></script>
</head>

<body>
    <div id="app">
        <commentbox @send_comment="sendComment"></commentbox>
        <div class="container">
            <ul class="list-group">
                <li class="list-group-item" v-for="item in list" :key="item.id">
                    <span class="badge"> {{ item.user }} </span>
                    {{ item.content }}
                </li>
            </ul>
        </div>
    </div>

    <template id="tmpl">
        <div class="container">
            <div class="panel panel-primary">
                <div class="panel-body">
                    <div class="form-group">
                        <label for="user">评论人：</label>
                        <input type="text" class="form-control" v-model="user" id="user" placeholder="评论人">
                    </div>

                    <div class="form-group">
                        <label for="content">评论内容：</label>
                        <textarea class="form-control" v-model="content" id="content"></textarea>
                    </div>
                    <input type="button" class="btn btn-primary" @click="postComment" value="提交">
                </div>
            </div>
        </div>
    </template>

    <script>
        var FORMAT_TIME_STR = 'YYYYMMDDHHmmssSSS';
        var genId = function () {
            var timeStr = moment().format(FORMAT_TIME_STR);
            var randomStr = Math.floor(Math.random() * 1000);
            return timeStr + randomStr;
        };
        var commentbox = {
            template: '#tmpl',
            data(){
                return {
                    user: '',
                    content: ''
                };
            },
            methods: {
                postComment() {
                    // console.log(this.user, this.content);
                    this.$emit('send_comment', {user:this.user, content: this.content});
                    this.content = this.user = '';
                }
            }
        };
        var vm = new Vue({
            el: '#app',
            data: {
                list: [
                    { id: genId(), user: '李白', content: '天生我材必有用' },
                    { id: genId(), user: '江小白', content: '劝君更来一杯酒' }
                ]
            },
            methods: {
                sendComment(comment) {
                    // console.log('parent: ', comment.user, comment.content);
                    var _comment = Object.assign({}, comment);
                    _comment.id = genId();
                    // this.list.push(_comment);
                    this.list.unshift(_comment);
                }
            },
            components: {
                commentbox
            }
        });
    </script>
</body>

</html>